package com.example.singletask.util

import java.lang.Exception

class Task {

    companion object{
        /**
         * 创建任务
         */
        fun <T> singleTask(runnable: (SingleTask<*>) -> T): SingleTask<T> {
            return object : SingleTask<T>(){
                @Throws(Exception::class)
                override fun onTaskRun(): T {
                    return runnable(this)
                }
            }
        }

        /**
         * 创建任务（带进度条）
         */
        fun <P,R> progressTask(runnable: (ProgressTask<P, R>) -> R): ProgressTask<P, R> {
            return object : ProgressTask<P, R>(){

                @Throws(Exception::class)
                override fun onTaskRun(): R {
                    return runnable(this)
                }
            }
        }

        /**
         * 创建计时器
         */
        fun timerTask(initialDelay: Long = 10,
                       interval: Long = 1000,
                       action: (timer: TimerTask, tick: TimerTask.Tick) -> Unit): TimerTask {

            return TimerTask(initialDelay, interval, action)
        }
    }
}